// import { useToken } from '@/stores/login'
import cookie from "js-cookie";
import { createRouter, createWebHistory } from 'vue-router'
import nprogress from 'nprogress' // 进度条
import 'nprogress/nprogress.css' //样式必须引入

const IndexView = () => import("@/views/Index.vue")
const Login = () => import("@/views/Login.vue")
const NoteFoundView = () => import("@/views/404.vue")

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/index/dashboard'
    },
    {
      path: '/index',
      name: 'index',
      component: IndexView,
      meta: {
        title: '首页'
      }
    },
    {
      path: '/Login',
      name: 'login',
      component: Login
    },
    {
      path: '/404',
      name: 'NoteFoundView',
      component: NoteFoundView
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404'
    }
  ]
})

router.beforeEach((to, from) => {
  const token = cookie.get('token')
  if (!token && to.name != 'login') {
    // console.log(token, 'name1111');
    router.replace({ path: "/login", query: { back: to.fullPath } })//添加查询参数
    return false
  }
  // console.log(token, 'name2222');
  return true
  nprogress.start()
})
router.afterEach(() => {
  nprogress.done()
})
export default router
